package com.customization.unionpay.market.dao;

import com.alibaba.fastjson.JSONObject;
import com.alitriph5.logging.Logger;
import com.alitriph5.logging.LoggerFactory;
import com.customization.unionpay.market.util.FormUtil;
import weaver.conn.RecordSet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ActivityApplicationDao {
    public Logger log = LoggerFactory.getLogger(ActivityApplicationDao.class);
    public List<JSONObject> queryByBdhs (List<String> bdh){
        //查询传过来的要校验的营销活动事项
        StringBuilder  querySql = new StringBuilder();
        querySql.append("SELECT ")
                .append("a.requestid, ")
                .append("a.PROJECT_CODE, ")
                .append("b.yskm, ")
                .append("b.yszrzx, ")
                .append("b.nbczje, ")
                .append("a.bdh, ")
                .append("a.yskzq, ")
                .append("a.ywlx, ")
                .append("a.sqr, ")
                .append("a.yxptbz, ")
                .append("a.bzgs, ")
                .append("a.bzbm, ")
                .append("b.jjsx ")
                .append("FROM ")
                .append(com.customization.unionpay.market.util.FormUtil.ACTIVITY_APPLICATION)
                .append(" a ")
                .append("LEFT JOIN ")
                .append(com.customization.unionpay.market.util.FormUtil.ACTIVITY_APPLICATION_DETAILS)
                .append(" b ON a.id = b.mainId ")
                .append("WHERE a.bdh IN (");
        bdh.forEach(o->{
            querySql.append(o).append(",");
        });
        querySql.deleteCharAt(querySql.length()-1).append(")");
        try {
            RecordSet rs = new RecordSet();
            rs.execute(querySql.toString());
            List<JSONObject> applicationList = new ArrayList<>();
            while (rs.next()){
                JSONObject o = new JSONObject();
                o.put("requestid",rs.getString("requestid"));
                o.put("PROJECT_CODE",rs.getString("PROJECT_CODE"));
                o.put("yskm",rs.getString("yskm"));
                o.put("yszrzx",rs.getString("yszrzx"));
                o.put("ysqjn",rs.getString("yskzq").substring(0,4));
                o.put("nbczje",rs.getString("nbczje"));
                o.put("bdh",rs.getString("bdh"));
                o.put("yskzq",rs.getString("yskzq"));
                o.put("ywlx",rs.getString("ywlx"));
                o.put("sqr",rs.getString("sqr"));
                o.put("lyxt","智慧财务系统");
                o.put("sm",rs.getString("yxptbz"));
                o.put("bzgs",rs.getString("bzgs"));
                o.put("bzbm",rs.getString("bzbm"));
                o.put("jjsx",rs.getString("jjsx"));
                applicationList.add(o);
            }
            return applicationList;
        }catch (Exception e){
            log.error("根据表单号查询营销事项申请调整单失败");
        }
        return null;
    }

    public Map<String,Object> queryBySqm(String sqm){
        //查询传过来的要校验的营销活动事项
        StringBuilder  querySql = new StringBuilder();
        querySql.append("SELECT ")
                .append("a.requestid, ")
                .append("a.PROJECT_CODE, ")
                .append("a.ywrq ")
                .append("b.yskm, ")
                .append("b.yszrzx, ")
                .append("b.nbczje, ")
                .append("a.bdh, ")
                .append("a.yskzq, ")
                .append("a.ywlx, ")
                .append("a.sqr, ")
                .append("a.yxptbz, ")
                .append("a.bzgs, ")
                .append("a.bzbm, ")
                .append("b.jjsx ")
                .append("FROM ")
                .append(com.customization.unionpay.market.util.FormUtil.ACTIVITY_APPLICATION)
                .append(" a ")
                .append("LEFT JOIN ")
                .append(FormUtil.ACTIVITY_APPLICATION_DETAILS)
                .append(" b ON a.id = b.mainId ")
                .append("WHERE a.yxpthdsqm =")
                        .append(sqm);
        Map<String, Object> o = new HashMap<>();
        try {
            RecordSet rs = new RecordSet();
            rs.execute(querySql.toString());

            while (rs.next()){
                o.put("requestid",rs.getString("requestid"));
                o.put("PROJECT_CODE",rs.getString("PROJECT_CODE"));
                o.put("yskm",rs.getString("yskm"));
                o.put("yszrzx",rs.getString("yszrzx"));
                o.put("ysqjn",rs.getString("yskzq").substring(0,4));
                o.put("nbczje",rs.getString("nbczje"));
                o.put("bdh",rs.getString("bdh"));
                o.put("yskzq",rs.getString("yskzq"));
                o.put("ywlx",rs.getString("ywlx"));
                o.put("sqr",rs.getString("sqr"));
                o.put("lyxt","智慧财务系统");
                o.put("sm",rs.getString("yxptbz"));
                o.put("bzgs",rs.getString("bzgs"));
                o.put("bzbm",rs.getString("bzbm"));
                o.put("jjsx",rs.getString("jjsx"));
            }

        }catch (Exception e){
            log.error("根据表单号查询营销事项申请调整单失败");
        }
        return o;
    }

}
